package com.mhkj.base.itemInfo.phase.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.itemInfo.phase.dao.PhaseDao;
import com.mhkj.base.itemInfo.phase.entity.Phase;
import com.mhkj.base.itemInfo.phase.service.PhaseService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class PhaseServiceImpl extends BaseServiceImpl<Phase> implements PhaseService {
	@Resource
	private PhaseDao phaseDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setPhaseDao(PhaseDao PhaseDao) {
		super.setBaseRepository(PhaseDao);
	}

	@Override
	public Pagination<Phase> listByPage(Phase Phase, int pageIndex, int size ) {
		Specification<Phase> spec = new Specification<Phase>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<Phase> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				
//				 Join<Object, Object> tSysNation = root.join("tSysNation",JoinType.INNER);
//
//				if (!StringUtils.isEmpty(Phase.getCityId())) {
//					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),Phase.getCityId()));
//				}
//				
//				if (!StringUtils.isEmpty(Phase.getName())) {
//					lstPredicates.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + Phase.getName() + "%"));
//				}
//				 
//				if (!StringUtils.isEmpty(Phase.getAffiliatedCompanyId())) {
//					lstPredicates.add(criteriaBuilder.equal(root.get("affiliatedCompanyId").as(String.class), Phase.getAffiliatedCompanyId()));
//				}
//				
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<Phase> pageT = phaseDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<Phase> pagination = new Pagination<Phase>();
		pagination.setTotal(phaseDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public List<Phase> findByCidOrItemName(Long cityId, String itemName,Long provinceId) {
		Specification<Phase> spec = new Specification<Phase>() {

			/**
			 *  序列号
			 */
			private static final long serialVersionUID = -2632227625581492703L;

			@Override
			public Predicate toPredicate(Root<Phase> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				
				 Join<Object, Object> tSysNation = root.join("tSysNation",JoinType.INNER);
				 Join<Object, Object> item = root.join("item",JoinType.INNER);

				if (!StringUtils.isEmpty(cityId)) {
					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),cityId));
				}
				if (!StringUtils.isEmpty(provinceId)) {
					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("parentId"),provinceId));
				}
				
				if (!StringUtils.isEmpty(itemName)) {
					lstPredicates.add(criteriaBuilder.like(item.get("name").as(String.class), "%" + itemName + "%"));
				}
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		 List<Phase> findAll = phaseDao.findAll(spec);
		return findAll;
	}

	@Override
	public List<Phase> findPhaseByItemId(Long itemId) {
		// TODO Auto-generated method stub
		return phaseDao.findPhaseByItemId( itemId);
	}

	@Override
	public List<Phase> findPhaseByGroupId(Long groupId) {
		// TODO Auto-generated method stub
		return phaseDao.findPhaseByGroupId( groupId);
	}
 

}
